<template>
  <div class="white-bg commonBoxmp">
    <TableV2
      class="flex1"
      :apiFunction="listNotice"
      ref="tableV2Ref"
      :columns="state.columnData"
    >
      <template #tableHeader>
        <div class="jcb aic w100">
          <h3 class="mx15">岗位管理</h3>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </div>
      </template>

      <template #operation>
        <el-table-column label="操作" fixed="right" width="160px" align="left">
          <template #default="scope">
            <el-button type="text" @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </template>
    </TableV2>

    <el-dialog
      v-model="state.userVisible"
      :title="state.title"
      width="50%"
      destroy-on-close
      center
    >
      <el-form
        ref="ruleFormRef"
        :model="state.form"
        :rules="state.rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input
                v-model="state.form.noticeTitle"
                placeholder="请输入公告标题"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <selectDict
                class="w100"
                dictCode="sys_notice_type"
                v-model="state.form.noticeType"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <selectDict
                class="w100"
                dataType="radio"
                dictCode="sys_notice_status"
                v-model="state.form.status"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="内容" prop="noticeContent">
              <el-input
                v-model="state.form.noticeContent"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="state.userVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="tsx">
import { ref, reactive, getCurrentInstance, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import Column from "/@ts/components/table/Column";
import selectDict from "/@/components/selectDict/index.vue";
import DictTag from "/@/components/DictTag/index.vue";
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "/@ts/axios/urlApi/system/notice";
import formatTool from "/@ts/hooks/format";
const router = useRouter();
const route = useRoute();
const { parseTime, addDateRange, getDict } = formatTool();
/*
基本数据类型
引用数据类型（复杂类型） 个人建议 ref初始化变量 
ref 和 reactive 本质我们可以简单的理解为ref是对reactive的二次包装, 
ref定义的数据访问的时候要多一个.value
*/
const dialogVisible = ref(true);
const state: any = reactive({
  // 表格基础配置 start
  form: {},
  title: "",
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
  dateRange: [], //时间范围筛选

  height: 0,
  tableData: [],
  columnData: [
    {
      label: "公告标题",
      prop: "noticeTitle",
      visible: true,
    },
    {
      label: "公告类型",
      prop: "noticeType",
      render: ({ row }: any) => {
        return (
          <DictTag
            values={row.noticeType}
            dataType="primary"
            options={dict.sys_notice_type}
          ></DictTag>
        );
      },
      visible: true,
    },
    {
      label: "状态",
      prop: "status",
      visible: true,
      render: ({ row }: any) => {
        return (
          <DictTag
            values={row.status}
            dataType="primary"
            options={dict.sys_notice_status}
          ></DictTag>
        );
      },
    },
    {
      label: "创建者",
      prop: "createBy",
      visible: true,
    },
    {
      label: "创建时间",
      prop: "createTime",
      visible: true,
    },
  ],
  loading: false,
  // 表格基础配置 end
});
// 表格基础配置 start
const pageInfo = reactive<PageInfoConfig>({
  currentPage: 1,
  total: 10,
  pageSize: 10,
  pageSizes: [10, 20, 30, 40],
  layout: "total, prev, pager, next,sizes",
});
const queryParams: any = reactive({
  pageNum: 1,
  pageSize: 10,
  deptId: undefined,
});
const getCurrentColumns = (data: Column[]) => {
  state.columnData = data;
};
const getHeight = (height: number) => {
  state.height = height;
};
const handleSizeChange = (size: number) => {
  pageInfo.pageSize = size;
  getList();
};
const handleCurrentChange = (page: number) => {
  pageInfo.currentPage = page;
  getList();
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate();
  if (state.form.noticeId != undefined) {
    updateNotice(state.form).then((response: ResponseConfig) => {
      ElMessage.success("修改成功");
      state.userVisible = false;
      getList();
    });
  } else {
    addNotice(state.form).then((response: ResponseConfig) => {
      ElMessage.success("新增成功");
      state.userVisible = false;
      getList();
    });
  }
};
const handleDelete = (row: any) => {
  const noticeId = row.noticeId;
  ElMessageBox.confirm('是否确认删除编号为"' + noticeId + '"的数据项？')
    .then(function () {
      return delNotice(noticeId);
    })
    .then(() => {
      ElMessage.success("删除成功");
      getList();
    })
    .catch(function () {});
};
/** 修改按钮操作 */
const handleUpdate = (row: any) => {
  const noticeId = row.noticeId;
  getNotice(noticeId).then((response: ResponseConfig) => {
    state.title = "修改通告";
    state.form = response.data;
    state.userVisible = true;
  });
};
const handleAdd = () => {
  state.form = {};
  state.title = "添加通告";
  state.userVisible = true;
};
// 表格基础配置 end
//查询用户列表
const getList = () => {
  state.loading = true;
  queryParams.pageNum = pageInfo.currentPage;
  queryParams.pageSize = pageInfo.pageSize;
  listNotice(addDateRange(queryParams, state.dateRange)).then(
    (response: ResponseConfig) => {
      state.tableData = response.rows;
      pageInfo.total = response.total;
      state.loading = false;
    },
  );
};

let dict: any = reactive({
  sys_notice_status: [],
  sys_notice_type: [],
});
const ruleFormRef = ref<FormInstance>();
onMounted(async () => {
  dict.sys_notice_status = await getDict("sys_notice_status");
  dict.sys_notice_type = await getDict("sys_notice_type");
  // getList()
});
</script>

<style scoped lang="scss"></style>
